#!/usr/bin/ruby -w

require 'optparse'
require 'socket'

OPTIONS = {
  :interface => '',
  :port      => 7777,
  :size      => 50000,
}

ARGV.options do |opts|
  script_name = File.basename $0
  opts.banner = "Usage: ruby #{script_name} [options]"

  opts.separator ""

  opts.on("-i", "--interface=interface", String,
	  "Binds to specified interface.",
	  "Default: all interfaces") { |OPTIONS[:interface]| }
  opts.on("-p", "--port=port", Integer,
	  "Binds to specified port.",
	  "Default: 7777") { |OPTIONS[:port]| }
  opts.on("-s", "--size=size", Integer,
	  "Chunk size for reading from socket.",
	  "Default: 50000") { |OPTIONS[:size]| }

  opts.separator ""

  opts.on("-h", "--help",
          "Show this help message.") { puts opts; exit }
  
  opts.parse!
end

socket = UDPSocket.new
socket.bind OPTIONS[:interface], OPTIONS[:port]
print socket.recv(OPTIONS[:size])
print socket.recv(OPTIONS[:size]) while select [socket], nil, nil, 1

